package com.bytz.modules.cms.domain.model.price;

import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.reseller.entity.ResellerProduct;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
public class PriceComputeContext {

    public PriceComputeContext(List<Product> inputProducts, String inputResellerId) {
        if (CollectionUtils.isNotEmpty(inputProducts)) {
            this.inputProducts = inputProducts.stream().collect(Collectors.toMap(Product::getProductNumber, Function.identity()));

            this.results = inputProducts.stream()
                    .map(p -> {
                        PriceComputeResult priceComputeResult = new PriceComputeResult();
                        // 放置与价格相关的属性
                        priceComputeResult.setProductNumber(p.getProductNumber());
                        priceComputeResult.setBasePrice(p.getBasePrice());
                        priceComputeResult.setProductLineId(p.getProductLineId());
                        priceComputeResult.setDiscountCategoryId(p.getDiscountCategoryId());
                        return priceComputeResult;
                    }).collect(Collectors.toMap(PriceComputeResult::getProductNumber, v -> v));
        }
        this.inputResellerId = inputResellerId;
    }


    /**
     * 要计算的商品
     */
    private Map<String, Product> inputProducts = Collections.emptyMap();

    public Product getByProductCode(String productCode) {
        return this.inputProducts.get(productCode);
    }

    /**
     * 经销商Id
     */
    private String inputResellerId;

    /**
     * 结果集
     */
    private Map<String, PriceComputeResult> results = Collections.emptyMap();

    /**
     * 是否需要继续
     */
    private Boolean needContinue = true;


    /**
     * 经销商专属折扣
     */
    private List<ResellerProduct> resellerProducts;

    /**
     * 经销商等级，Key是产品线Id，Value是产品线等级Id
     */
    private Map<String, String> resellerLevelMap;

    public Set<String> getHasPricePns() {
        return this.results.values().stream()
                .filter(p -> p.getPrice() != null)
                .map(PriceComputeResult::getProductNumber)
                .collect(Collectors.toSet());
    }
}